jQuery(document).ready(function($) {
   $("#verifytokenform #email").jobUtil("emailProvider");

   $("#verifytokenform").validate({
      submitHandler: function(form) {
         $("#verifytokenform").ajaxSubmit({
            dataType: "json",
            beforeSubmit: function(data, form, options) {
               if (!showLoading("#verifytokenform"))
                  return false;
               $("#errormsg").hide();
            },
            success: function(data) {
               try {
                  var retmsg = data;
                  if (retmsg.error) {
                     $("#errormsg").jobUtil("showError", retmsg.error);
                  }
                  if (retmsg.success) {
                     window.location = retmsg.redirect;
                  }
               } catch(e) {
                  return false;
               }
            }
         });
      },
      rules: {
         phone: {required: true, phone: true},
         email: {required: true, email: true}
      },
      messages: {
         phone: '请输入正确格式的手机号码',
         email: '请输入正确格式的电子邮件'
      },
      failure: function(label) {
         label.removeClass("checked"); 
         if(label.attr("for") == "email") {
            $("#verifytokenform #reg_vcode").hide();
         }
      },
      success: function(label) {
         if(label.attr("for") == "vcode") {
            return; 
         }
         label.addClass("checked");
         if(label.attr("for") == "email") {
            $("#verifytokenform #reg_vcode").show();
            var email = $.trim($("#verifytokenform #email").val()); 
            if(!$("#verifytokenform #reg_getvcode").data("email"))
               $("#verifytokenform #reg_getvcode").data("email", email).trigger("click");
         }
      },
   });

   $("#verify_submit").prop('disabled',false);
   
   $("#verifytokenform #reg_getvcode").click(function(e) {
      $(this).jobUtil("getVerifyCode", {
         url: getvcodeurl,
         email: $.trim($("#verifytokenform #email").val())
      });
   })
   // focus first text-field 
   setFocus("verifytokenform");
});
